package com.nuoniu.sibanyun.mapper.crm;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.annotation.DataAuth;
import com.nuoniu.sibanyun.entity.bo.CrmBusinessBo;
import com.nuoniu.sibanyun.entity.crm.CrmBusiness;
import com.nuoniu.sibanyun.entity.dto.SaleFunnelDto;
import com.nuoniu.sibanyun.entity.vo.CrmBusinessVo;
import com.nuoniu.sibanyun.mapper.provider.CrmMapperProvider;
import com.nuoniu.sibanyun.userThread.DataScope;
import com.nuoniu.sibanyun.userThread.UserInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 客户商机 Mapper 接口
 * </p>
 *
 * @author baiqiang
 * @since 2020-07-08
 */
public interface CrmBusinessMapper extends BaseMapper<CrmBusiness> {

    /**
     * 分页获取商机列表
     * @author 小啊刚
     * @date 2020/7/8
     */
    @SelectProvider(type = CrmMapperProvider.class, method = "pageBusiness")
    @DataAuth(tableAlias = "crm",personalField = "collar_user_id",companyField = "company_id",officeField = "office_id")
    Page<CrmBusiness> pageBusiness(Page page, @Param("business") CrmBusiness business, @Param("user") UserInfo user);

    /**
     * 检查 租户下的 商机编号是否重复
     * @param projectNo
     * @param companyId
     * @return
     */
    @Select("SELECT COUNT(1) FROM crm_business bs INNER JOIN pub_customer crm ON crm.id = bs.crm_id AND crm.company_id = #{companyId} WHERE project_no = #{projectNo}")
    boolean isHaveNo(String projectNo,Integer companyId);

    /**
     * 销售漏斗统计
     * @param dataScope
     * @param saleFunnelDto
     * @return
     */
    List<CrmBusinessBo> saleFunnelStatistic(DataScope dataScope, SaleFunnelDto saleFunnelDto);

    @Select("SELECT b.*,crm.crm_type FROM crm_business b INNER JOIN pub_customer crm on crm.id = b.crm_id AND b.`status` = 0 AND crm.`status` = 0 AND b.id = #{id}")
    CrmBusinessVo detailById(Integer id);

    /**
     * 获取最新一条数据
     */
    @Select("select bs.* from crm_business bs\n" +
            "\tINNER JOIN pub_customer crm ON crm.id = bs.crm_id \n" +
            "\tAND crm.company_id = #{companyId} \n" +
            "\twhere  date(bs.create_date) = #{createDate} order by bs.create_date desc limit 1")
    CrmBusiness getFirstCrmBusiness(Integer companyId, String createDate);

    /**
     * 修改商机编码
     * @author 小啊刚
     * @date 2020-08-11
     */
    @Update("update crm_business b left join pub_customer c on b.crm_id = c.id " +
            "set b.project_no = REPLACE(b.project_no, left(b.project_no, length(b.project_no) - 12), #{currentCode}) where c.company_id = #{companyId}")
    void updateCode(String currentCode, Integer companyId);



    List<CrmBusiness> listNoDistributeCrmBusiness(Integer companyId);

    List<CrmBusiness> listActivityBusiness(Integer activityId,Integer companyId);

    @Select("select project_no from crm_business where company_id = #{companyId} and date_format(create_date, #{dateStr})=date_format(now(), #{dateStr}) order by create_date desc limit 1")
    String getNewestCode(Integer companyId, String dateStr);
}
